options ls=72 ps=54 spool mlogic mprint;

libname b "/Young_Surgeon_data/sas/sharpe/partb_linkage";
libname z "/Young_Surgeon_data/sas/zhou/data";
libname m "/Young_Surgeon/sas_pgm/sharpe/match";
libname hc "/Young_Surgeon_data/sas/hill/check";
libname d "/Young_Surgeon_data/sas/hill/comorb";
libname lf "/Young_Surgeon_data/sas/sharpe/match/link_files";
libname n "/Young_Surgeon_data/sas/NPI";
libname ho "/Young_Surgeon_data/sas/hill/outcomes";
libname hh "/Young_Surgeon_data/sas/hill/hosps";
libname hc "/Young_Surgeon_data/sas/hill/comorb";
libname hch "/Young_Surgeon_data/sas/hill/check";

*******************************;
**** Read in the AMA file. ****;


proc sort data=z.surgeon_final_to_james_11022018 out=ama_abms_upin nodupkey;
 by upin; run;
 
proc sort data=z.surgeon_final_to_james_11022018 out=ama_abms_npi nodupkey;
 by npi; run;

proc contents data=ama_abms_npi;
run;
data ama_abms_upin;
 set ama_abms_upin;
 where upin ne "";
 run;

data ama_abms_npi;
 set ama_abms_npi;
 where npi ne "";
 run;
 
 
**************************************************;
**** Change the new surgeon flags to numeric. ****;
 
%macro change_flags(id=);

data ama_abms_&id.;
set ama_abms_&id.;

format Mod_New_Surgeon_flag_numeric 8.
	Mod_Exp_Surgeon_flag_numeric 8.
	Trad_New_Surgeon_flag_numeric 8.
	Trad_Exp_Surgeon_flag_numeric 8.;

Mod_New_Surgeon_flag_numeric=0;
Mod_Exp_Surgeon_flag_numeric=0;
Trad_New_Surgeon_flag_numeric=0;
Trad_Exp_Surgeon_flag_numeric=0;

if Mod_New_flag_final_11022018 = "1" then Mod_New_Surgeon_flag_numeric=1;
if Mod_Exp_flag_final_11022018 = "1" then Mod_Exp_Surgeon_flag_numeric=1;
if Trad_New_flag_final_11022018 = "1" then Trad_New_Surgeon_flag_numeric=1;
if Trad_Exp_flag_final_11022018 = "1" then Trad_Exp_Surgeon_flag_numeric=1;

run;


data ama_abms_&id.;
set ama_abms_&id.;
keep Mod_Exp_Surgeon_flag_numeric
	Mod_New_Surgeon_flag_numeric
	Trad_Exp_Surgeon_flag_numeric
	Trad_New_Surgeon_flag_numeric
	Mod_Exp_flag_final_11022018
	Mod_New_flag_final_11022018
	Trad_Exp_flag_final_11022018
	Trad_New_flag_final_11022018
	YOG YOT YOT_flag ResearchID UPIN NPI
	Board_Gen_surgery_flag
	Board_ID1 Board_ID2 Board_ID3 Board_ID4 Board_ID5
	Board_Name1 Board_Name2 Board_Name3 Board_Name4 Board_Name5
	DOFlag LOT_miss_aberrant LengthOFTraining
	MED_TRFROM MedSchoolYOG PrimarySpecialtyCode
	PrimarySpecialtyName Specialty_Gen_surgery_flag
	Specialty_Orth_surgery_flag
	YOB board_Orth_surgery_flag sex;
run;

*************************************************************************;
**** Exclude surgeons if they did not have 1 of the 4 surgeon flags. ****;

data ama_abms_&id.;
set ama_abms_&id.;
if Mod_New_Surgeon_flag_numeric^=0 or Mod_Exp_Surgeon_flag_numeric^=0 or
	Trad_New_Surgeon_flag_numeric^=0 or Trad_Exp_Surgeon_flag_numeric^=0;
run;


%mend change_flags;

%change_flags(id=NPI);
%change_flags(id=UPIN);
 
data ama_abms_npi;
set ama_abms_npi;
drop UPIN;
run;
 
 

***********************************************************;
**** Merge in the NPI_flag variable from the NPI file. ****; 

data clean_npi;
set n.clean_npi_12262016_flag;
if Best_NPI_1213_J^=.;
keep Best_NPI_1213_J NPI_FLAG;
run;

data clean_npi;
set clean_npi;
rename Best_NPI_1213_J=NPI;
run;

proc sort data=clean_npi nodupkey;
by NPI;
run;



data ama_abms_npi;
merge ama_abms_npi (in=a) clean_npi (in=b);
by NPI;
if a=1;
if a=1 and b=0 then NPI_FLAG=0;
run;

proc freq data=ama_abms_npi;
table NPI_FLAG / missing;
run;

proc sort data=ama_abms_upin;
by NPI;
run;

data ama_abms_upin;
merge ama_abms_upin (in=a) clean_npi (in=b);
by NPI;
if a=1;
if a=1 and b=0 then NPI_FLAG=0;
run; 

proc freq data=ama_abms_upin;
table NPI_FLAG / missing;
run;

data ama_abms_upin;
set ama_abms_upin;
drop NPI;
run;

proc sort data=ama_abms_upin nodupkey;
by UPIN;
run;

proc sort data=ama_abms_npi nodupkey;
by NPI;
run;


*******************************************************;
**** Read in the data and merge with the AMA file. ****;

%macro read_data(era=,surg=,id=);

data merged_&era._&surg.;
set b.merged_wcw_&era._&surg.;
ama_flag=0;
drop spx_major_therapeutic spx_major_diagnostic
	spx_minor_therapeutic spx_minor_diagnostic
	spx_none;
run;

**** Only keep surgeons who appeared as either a new or exp surgeon in the same ****;
**** era as the inpatient dataset. *************************************************;

data ama_abms_&id._2;
set ama_abms_&id.;
if &era._New_Surgeon_flag_numeric=1 or &era._Exp_Surgeon_flag_numeric=1;
run;

proc contents data=ama_abms_&id.;
run;

data merged_&era._&surg. (drop=rc);
	declare hash plan ();
	
	rc=plan.definekey("&id.");
	rc=plan.definedata("Mod_Exp_Surgeon_flag_numeric",
			"Mod_New_Surgeon_flag_numeric",
			"Trad_Exp_Surgeon_flag_numeric",
			"Trad_New_Surgeon_flag_numeric",
			"Mod_Exp_flag_final_11022018",
			"Mod_New_flag_final_11022018",
			"Trad_Exp_flag_final_11022018",
			"Trad_New_flag_final_11022018",
			"YOG", "YOT", "YOT_flag",
			"ResearchID", "Board_Gen_surgery_flag",
			"Board_ID1", "Board_ID2", "Board_ID3",
			"Board_ID4", "Board_ID5", "Board_Name1",
			"Board_Name2", "Board_Name3", "Board_Name4",
			"Board_Name5", "DOFlag", "LOT_miss_aberrant",
			"LengthOFTraining", "MED_TRFROM", "MedSchoolYOG",
			"PrimarySpecialtyCode",	"PrimarySpecialtyName",
			"Specialty_Gen_surgery_flag",
			"Specialty_Orth_surgery_flag", "YOB",
			"board_Orth_surgery_flag", "sex");
	rc=plan.definedone();
	
	do until (eof1);
		set ama_abms_&id._2 end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(Mod_Exp_Surgeon_flag_numeric,
			Mod_New_Surgeon_flag_numeric,
			Trad_Exp_Surgeon_flag_numeric,
			Trad_New_Surgeon_flag_numeric,
			Mod_Exp_flag_final_11022018,
			Mod_New_flag_final_11022018,
			Trad_Exp_flag_final_11022018,
			Trad_New_flag_final_11022018,
			YOG, YOT, YOT_flag,
			ResearchID, Board_Gen_surgery_flag,
			Board_ID1, Board_ID2, Board_ID3,
			Board_ID4, Board_ID5, Board_Name1,
			Board_Name2, Board_Name3, Board_Name4,
			Board_Name5, DOFlag, LOT_miss_aberrant,
			LengthOFTraining, MED_TRFROM, MedSchoolYOG,
			PrimarySpecialtyCode,	PrimarySpecialtyName,
			Specialty_Gen_surgery_flag,
			Specialty_Orth_surgery_flag, YOB,
			board_Orth_surgery_flag, sex);
		rc=plan.find();
		if plan.find() = 0 then ama_flag=1;
		output;
	end;
	
	stop;
	
run;


data merged_&era._&surg.;
set merged_&era._&surg.;
count=1;
oct_2010_plus=0;
jan_2012_plus=0;
imp_date_1 = "01OCT2010"d;
imp_date_2 = "01JAN2012"d;
if CLM_ADMSN_DT >= imp_date_1 then oct_2010_plus=1;
if CLM_ADMSN_DT >= imp_date_2 then jan_2012_plus=1;
age_at_admsn_floor = floor(age_at_admsn);
age_at_admsn_dec = age_at_admsn - age_at_admsn_floor;

if age_at_admsn_dec < 0.25 then quarter_year=0.00;
else if age_at_admsn < 0.50 then quarter_year=0.25;
else if age_at_admsn < 0.75 then quarter_year=0.50;
else quarter_year=0.75;

age_at_admsn = age_at_admsn_floor + quarter_year;

partb_year = year(CLM_THRU_DT_line);
partb_month = month(CLM_THRU_DT_line);

if partb_month <= 3 then partb_quarter=0.00;
else if partb_month <= 6 then partb_quarter=0.25;
else if partb_month <= 9 then partb_quarter=0.50;
else partb_quarter = 0.75;

admsn_in_quarters = partb_year + partb_quarter;

weekend=(weekday(CLM_ADMSN_DT) in (1,7));

basic_los = NCH_BENE_DSCHRG_DT - CLM_ADMSN_DT + 1;

if ama_flag=1;

run;

title "&era. &surg. tables";
proc freq data=merged_&era._&surg.;
tables admit_year*Trad_New_Surgeon_flag_numeric
	admit_year*Mod_New_Surgeon_flag_numeric;
run;
title "";

%mend read_data;


%read_data(era=trad, surg=ortho, id=UPIN);
%read_data(era=trad, surg=gensurg, id=UPIN);
%read_data(era=mod, surg=ortho, id=NPI);
%read_data(era=mod, surg=gensurg, id=NPI);





******************************************************************************;
***** Exclude cases if new surgeons were too far removed or exp. surgeons ****;
**** were not removed far enough. ********************************************;

%macro yot_ex(era=,surg=);

data merged_&era._&surg.;
set merged_&era._&surg.;

YOT_missing=0;
if YOT=. then YOT_missing=1;

if YOT_missing=0 then clm_minus_yot = (year(CLM_THRU_DT_line)-YOT);
else if YOT_missing=1 then clm_minus_yot = (year(CLM_THRU_DT_line)-YOG);

if YOT_missing=0 then exp_at_partb = (year(CLM_THRU_DT_line) - YOT);
else if YOT_missing=1 then exp_at_partb=(year(CLM_THRU_DT_line)-YOG-5);

YOT_YOG_flag=0;
if (&era._New_Surgeon_flag_numeric=1 and YOT_missing=0 and clm_minus_yot>3) then YOT_YOG_flag=1;
if (&era._New_Surgeon_flag_numeric=1 and YOT_missing=1 and clm_minus_yot>8) then YOT_YOG_flag=1;
if (&era._Exp_Surgeon_flag_numeric=1 and YOT_missing=0 and clm_minus_yot<10) then YOT_YOG_flag=1;
if (&era._Exp_Surgeon_flag_numeric=1 and YOT_missing=1 and clm_minus_yot<15) then YOT_YOG_flag=1;

run;

data merged_&era._&surg.;
set merged_&era._&surg.;
if YOT_YOG_flag=0;
run;

**** Check experience at part B. ****;
proc univariate data=merged_&era._&surg.;
var exp_at_partb;
run;


%mend yot_ex;

%yot_ex(era=mod, surg=ortho);
%yot_ex(era=mod, surg=gensurg);
%yot_ex(era=trad, surg=ortho);
%yot_ex(era=trad, surg=gensurg);


***********************;
**** Add outcomes. ****;

data death_30;
set d.death;
run;

data dual;
set ho.dual;
run;

data zip_acs_link;
set ho.zip_acs_link;
run;

data readm;
set ho.readms;
run;

data readms_alt;
set ho.readms_alt;
run;

data icu_days;
set ho.icu_days;
run;

data cost_anesth;
set ho.cost_anesth;
run;

data cost_run_costs;
set ho.cost_run_costs;
run;

data ftr;
set ho.ftr_calc;
run;

data los_long;
set ho.los_long;
run;

data plos_apply;
set ho.plos_apply;
run;

data silb_complic_inhosp;
set ho.silb_complic_inhosp;
run;

data silb_complic_poa_inhosp;
set ho.silb_complic_poa_inhosp;
run;

data silb_complic_poa_inhosp;
set silb_complic_poa_inhosp;
rename complic_inhosp_any=complic_poa_any;
run;

data step_up;
set ho.step_up;
run;

data hosp_chars;
set hh.hosp_chars;
run;

data spx_narrow;
set d.spx_major_narrow;
run;

data death_extra;
set hc.death_extra (keep=BENE_ID CLM_ID dead_60 dead_90 dead_180);
run;

data robotic;
set hch.robotic;
run;


%macro add_outcomes(era=, surg=);

*****************************************************************************************;
**** Merge the dataset with Alexs dataset that contains 30-day mortality (dead_30). ****;

data merged_&era._&surg. (drop=rc);
	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("dead_30");
	rc=plan.definedone();
	
	do until (eof1);
		set death_30 end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(dead_30);
		rc=plan.find();
		output;
	end;
	stop;
run;

data merged_&era._&surg.;
	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("dual");
	rc=plan.definedone();
	
	do until (eof1);
		set dual end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(dual);
		rc=plan.find();
		output;
	end;
	stop;
run;

data merged_&era._&surg.;
	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("median_income","pct_hsgrad");
	rc=plan.definedone();
	
	do until (eof1);
		set zip_acs_link end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(median_income, pct_hsgrad);
		rc=plan.find();
		output;
	end;
	stop;
run;



data merged_&era._&surg.;
	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("readms_30","readms_or_death","dead_inhosp");
	rc=plan.definedone();
	
	do until (eof1);
		set readm end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(readms_30, readms_or_death, dead_inhosp);
		rc=plan.find();
		output;
	end;
	stop;
run;

data merged_&era._&surg.;
	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("icu_days", "icu_yes");
	rc=plan.definedone();
	
	do until (eof1);
		set icu_days end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(icu_days, icu_yes);
		rc=plan.find();
		output;
	end;
	stop;
run;

data merged_&era._&surg.;
	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("anesth_time_claim");
	rc=plan.definedone();
	
	do until (eof1);
		set cost_anesth end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(anesth_time_claim);
		rc=plan.find();
		output;
	end;
	stop;
run;


data merged_&era._&surg.;
	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("total_cost_complete", "total_cost_index_only");
	rc=plan.definedone();
	
	do until (eof1);
		set cost_run_costs end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(total_cost_complete, total_cost_index_only);
		rc=plan.find();
		output;
	end;
	stop;
run;


data merged_&era._&surg.;
	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("ftr_30_ign_poa", "ftr_inhosp_ign_poa", "ftr_30_use_poa", "ftr_inhosp_use_poa");
	rc=plan.definedone();
	
	do until (eof1);
		set ftr end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(ftr_30_ign_poa, ftr_inhosp_ign_poa, ftr_30_use_poa, ftr_inhosp_use_poa);
		rc=plan.find();
		output;
	end;
	stop;
run;

data merged_&era._&surg.;
	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("los_long");
	rc=plan.definedone();
	
	do until (eof1);
		set los_long end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(los_long);
		rc=plan.find();
		output;
	end;
	stop;
run;




data merged_&era._&surg.;
	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("prolonged_d");
	rc=plan.definedone();
	
	do until (eof1);
		set plos_apply end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(prolonged_d);
		rc=plan.find();
		output;
	end;
	stop;
run;


data merged_&era._&surg.;
	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("complic_inhosp_BLEED","complic_inhosp_BONENECR",
		"complic_inhosp_BRONCHO", "complic_inhosp_CARDEM",
		"complic_inhosp_CHF", "complic_inhosp_COMA","complic_inhosp_COMPSYN",
		"complic_inhosp_DECUB","complic_inhosp_DIC","complic_inhosp_DVT",
		"complic_inhosp_GANGR","complic_inhosp_HEPAJUA","complic_inhosp_HYPO",
		"complic_inhosp_INTDAM","complic_inhosp_NERVCOMPR","complic_inhosp_NSCOMP",
		"complic_inhosp_OBSTRU","complic_inhosp_ORTHOCO","complic_inhosp_OSTEOM",
		"complic_inhosp_PANCR","complic_inhosp_PE","complic_inhosp_PERF",
		"complic_inhosp_PERITON","complic_inhosp_PHLEB","complic_inhosp_PNEUA",
		"complic_inhosp_PNEUAO","complic_inhosp_PNTHX","complic_inhosp_PSCOM",
		"complic_inhosp_PSYCHO","complic_inhosp_PYELON","complic_inhosp_RENAL",
		"complic_inhosp_REOP","complic_inhosp_RESP","complic_inhosp_SEIZ",
		"complic_inhosp_SEPSIS","complic_inhosp_STROKE","complic_inhosp_TIA",
		"complic_inhosp_Urin_tract","complic_inhosp_WOUND",
		"complic_inhosp_any");
	rc=plan.definedone();
	
	do until (eof1);
		set silb_complic_inhosp end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(complic_inhosp_BLEED, complic_inhosp_BONENECR,
			complic_inhosp_BRONCHO, complic_inhosp_CARDEM,
			complic_inhosp_CHF, complic_inhosp_COMA,complic_inhosp_COMPSYN,
			complic_inhosp_DECUB,complic_inhosp_DIC,complic_inhosp_DVT,
			complic_inhosp_GANGR,complic_inhosp_HEPAJUA,complic_inhosp_HYPO,
			complic_inhosp_INTDAM,complic_inhosp_NERVCOMPR,complic_inhosp_NSCOMP,
			complic_inhosp_OBSTRU,complic_inhosp_ORTHOCO,complic_inhosp_OSTEOM,
			complic_inhosp_PANCR,complic_inhosp_PE,complic_inhosp_PERF,
			complic_inhosp_PERITON,complic_inhosp_PHLEB,complic_inhosp_PNEUA,
			complic_inhosp_PNEUAO,complic_inhosp_PNTHX,complic_inhosp_PSCOM,
			complic_inhosp_PSYCHO,complic_inhosp_PYELON,complic_inhosp_RENAL,
			complic_inhosp_REOP,complic_inhosp_RESP,complic_inhosp_SEIZ,
			complic_inhosp_SEPSIS,complic_inhosp_STROKE,complic_inhosp_TIA,
			complic_inhosp_Urin_tract,complic_inhosp_WOUND,
			complic_inhosp_any);	
		rc=plan.find();
		output;
	end;
	stop;
run;


data merged_&era._&surg.;
	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("complic_poa_any");
	rc=plan.definedone();
	
	do until (eof1);
		set silb_complic_poa_inhosp end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(complic_poa_any);	
		rc=plan.find();
		output;
	end;
	stop;
run;



data merged_&era._&surg.;
	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("step_up");
	rc=plan.definedone();
	
	do until (eof1);
		set step_up end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(step_up);	
		rc=plan.find();
		output;
	end;
	stop;
run;

data merged_&era._&surg.;
	declare hash plan ();
	
	rc=plan.definekey("PRVDR_NUM");
	rc=plan.definedata("beds", "RB_Ratio", "nb_ratio", "nurse_mix",
				"cardiac_tech_triad", "cabg_or_organ_tx",
				"hcris_ok", "pos_ok", "rb_category",
				"size_category", "nb_category","nm_category",
				"rb_cat_missing", "rb_cat_nonteaching",
				"rb_cat_minor", "rb_cat_major",
				"size_cat_missing", "size_cat_small",
				"size_cat_medium", "size_cat_large",
				"nb_cat_missing", "nb_cat_low",
				"nb_cat_moderate", "nb_cat_high",
				"nm_cat_missing", "nm_cat_low",
				"nm_cat_moderate", "nm_cat_high");
	rc=plan.definedone();
	
	do until (eof1);
		set hosp_chars end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(beds, RB_Ratio, nb_ratio,
			nurse_mix, cardiac_tech_triad, cabg_or_organ_tx,
			hcris_ok, pos_ok, rb_category, size_category,
			nb_category, nm_category, rb_cat_missing,
			rb_cat_nonteaching, rb_cat_minor, rb_cat_major,
			size_cat_missing, size_cat_small, size_cat_medium,
			size_cat_large, nb_cat_missing, nb_cat_low,
			nb_cat_moderate, nb_cat_high, nm_cat_missing,
			nm_cat_low, nm_cat_moderate, nm_cat_high);	
		rc=plan.find();
		output;
	end;
	stop;
run;


data merged_&era._&surg.;

	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("spx_major_narrow");
	rc=plan.definedone();
	
	do until (eof1);
		set spx_narrow end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(spx_major_narrow);
		rc=plan.find();
		output;
	end;
	stop;
run;

data merged_&era._&surg.;

	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("dead_60", "dead_90", "dead_180");
	rc=plan.definedone();
	
	do until (eof1);
		set death_extra end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(dead_60, dead_90, dead_180);
		rc=plan.find();
		output;
	end;
	stop;
run;

data merged_&era._&surg.;

	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("readms30_post");
	rc=plan.definedone();
	
	do until (eof1);
		set readms_alt end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(readms30_post);
		rc=plan.find();
		output;
	end;
	stop;
run;

data merged_&era._&surg.;

	declare hash plan ();
	
	rc=plan.definekey("BENE_ID","CLM_ID");
	rc=plan.definedata("robotic_assist_spx");
	rc=plan.definedone();
	
	do until (eof1);
		set robotic end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set merged_&era._&surg. end=eof2;
		call missing(robotic_assist_spx);
		rc=plan.find();
		output;
	end;
	stop;
run;


data merged_&era._&surg.;
set merged_&era._&surg.;
format binary_pct_hsgrad 8.;
binary_pct_hsgrad=0;
if pct_hsgrad < 0.80 then binary_pct_hsgrad=1;
run;
	

%mend add_outcomes;

%add_outcomes(era=trad, surg=ortho);
%add_outcomes(era=trad, surg=gensurg);
%add_outcomes(era=mod, surg=ortho);
%add_outcomes(era=mod, surg=gensurg);

****************************************;
**** Set all the datasets together. ****;

data trad;
set merged_trad_ortho
	merged_trad_gensurg;
run;

data mod;
set merged_mod_ortho
	merged_mod_gensurg;
run;



*************************************************************;
**** Assign each surgeon to their majority surgery type. ****;

%macro assign_surgery(era=,id=);

proc sort data=&era.;
by &id.;
run;

proc means data=&era. noprint;
by &id.;
var ortho_adm_&era. gensurg_adm_&era.;
output out=surgery_&era. sum=sum_ortho sum_gensurg;
run;

data surgery_&era;
set surgery_&era.;
format maj_surg_type $7.;
if sum_gensurg >= sum_ortho then maj_surg_type="gensurg";
else maj_surg_type="ortho";
keep &id. maj_surg_type;
run;

data &era._1 (drop=rc);
	declare hash plan ();
	
	rc=plan.definekey("&id.");
	rc=plan.definedata("maj_surg_type");
	rc=plan.definedone ();
	
	do until (eof1);
		set surgery_&era. end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set &era. end=eof2;
		call missing(maj_surg_type);
		rc=plan.find();
		if ((gensurg_adm_&era.=1 and maj_surg_type="gensurg") or
			(ortho_adm_&era.=1 and maj_surg_type="ortho")) then output;
	end;
	
	stop;
run;
	



%mend assign_surgery;

%assign_surgery(era=trad, id=UPIN);
%assign_surgery(era=mod, id=NPI);

*********************************************************;
**** Assign each surgeon to their majority hospital. ****;

%macro assign_hospital(era=,id=);

proc sort data=&era._1;
by &id. PRVDR_NUM;
run;

proc means data=&era._1 noprint;
by &id. PRVDR_NUM;
var count;
output out=&era._hosp_sums sum=sum_count;
run;

proc sort data=&era._hosp_sums;
by &id. descending sum_count;
run;

proc sort data=&era._hosp_sums nodupkey;
by &id.;
run;

data &era._2;
	declare hash plan ();
	
	rc=plan.definekey("&id.", "PRVDR_NUM");
	rc=plan.definedata("sum_count");
	rc=plan.definedone();
	
	do until (eof1);
		set &era._hosp_sums end=eof1;
		rc=plan.add();
	end;
	
	do until (eof2);
		set &era._1 end=eof2;
		call missing(sum_count);
		rc=plan.find();
		if plan.find() = 0 then output;
	end;
	
	stop;
run;

%mend assign_hospital;

%assign_hospital(era=trad, id=UPIN);
%assign_hospital(era=mod, id=NPI);

****************************************************;
**** Randomly sample one admission per patient. ****;

data all;
set trad_2 mod_2;
run;

proc sort data=all;
by BENE_ID;
run;

options nonotes;
proc surveyselect data=all
	method=srs n=1 seed=112017 out=random_ys noprint;
strata BENE_ID;
run;
options notes;

data random_ys (drop=ppx_dt);
set random_ys (keep=AT_PHYSN_NPI AT_PHYSN_UPIN BENE_ID
	GNDR_CD ICD9_Label ICD_PRCDR_CD1 OP_PHYSN_NPI OP_PHYSN_UPIN PRVDR_NUM
	PRVDR_SPCLTY PTNT_DSCHRG_STUS_CD
	admit_year ama_flag count era_mod era_trad
	gensurg_adm_mod gensurg_adm_trad icdcpt_group_flag
	merge_ama_by_npi merge_ama_by_upin merge_pb ortho_adm_mod ortho_adm_trad
	same_date_flag NPI UPIN ppx_: admit_year emergent_type age_65_69 age_70_74
	age_75_79 age_80_85 age_85_plus age_at_admsn sex_female transfer_24hrs_ip
	silb_: oct_2010_plus jan_2012_plus past_adm_6mo CLM_ID
	spx: Narrow_Procedure_Group Broad_Category
	Mod_Exp_Surgeon_flag_numeric Mod_New_Surgeon_flag_numeric
	Trad_Exp_Surgeon_flag_numeric Trad_New_Surgeon_flag_numeric
	YOG YOT YOB ResearchID dead_30
	admsn_in_quarters NPI_FLAG exp_at_partb
	dual median_income pct_hsgrad binary_pct_hsgrad weekend
	readms_30 readms_or_death dead_inhosp icu_days icu_yes
	anesth_time_claim total_cost_complete total_cost_index_only
	ftr_30_ign_poa ftr_inhosp_ign_poa ftr_30_use_poa
	ftr_inhosp_use_poa los_long prolonged_d
	complic_inhosp_BLEED complic_inhosp_BONENECR complic_inhosp_BRONCHO
	complic_inhosp_CARDEM complic_inhosp_CHF complic_inhosp_COMA
	complic_inhosp_COMPSYN complic_inhosp_DECUB complic_inhosp_DIC
	complic_inhosp_DVT complic_inhosp_GANGR complic_inhosp_HEPAJUA
	complic_inhosp_HYPO complic_inhosp_INTDAM complic_inhosp_NERVCOMPR
	complic_inhosp_NSCOMP complic_inhosp_OBSTRU complic_inhosp_ORTHOCO
	complic_inhosp_OSTEOM complic_inhosp_PANCR complic_inhosp_PE
	complic_inhosp_PERF complic_inhosp_PERITON complic_inhosp_PHLEB
	complic_inhosp_PNEUA complic_inhosp_PNEUAO complic_inhosp_PNTHX
	complic_inhosp_PSCOM complic_inhosp_PSYCHO complic_inhosp_PYELON
	complic_inhosp_RENAL complic_inhosp_REOP complic_inhosp_RESP
	complic_inhosp_SEIZ complic_inhosp_SEPSIS complic_inhosp_STROKE
	complic_inhosp_TIA complic_inhosp_Urin_tract complic_inhosp_WOUND
	complic_inhosp_any complic_poa_any basic_los step_up beds RB_Ratio nb_ratio
	nurse_mix cardiac_tech_triad cabg_or_organ_tx
	hcris_ok pos_ok rb_category size_category
	nb_category nm_category rb_cat_missing
	rb_cat_nonteaching rb_cat_minor rb_cat_major
	size_cat_missing size_cat_small size_cat_medium
	size_cat_large nb_cat_missing nb_cat_low
	nb_cat_moderate nb_cat_high nm_cat_missing
	nm_cat_low nm_cat_moderate nm_cat_high
	Board_Gen_surgery_flag Board_ID1 Board_ID2 Board_ID3 Board_ID4
	Board_ID5 Board_Name1 Board_Name2 Board_Name3 Board_Name4 Board_Name5
	DOFlag LOT_miss_aberrant LengthOfTraining MED_TRFROM MedSchoolYOG
	PrimarySpecialtyCode PrimarySpecialtyName
	Specialty_Gen_surgery_flag Specialty_Orth_surgery_flag
	board_Orth_surgery_flag sex
	dead_60 dead_90 dead_180 
	readms30_post robotic_assist_spx);
run;

*******************************************;
**** Create psuedo-random identifiers. ****;

%macro ps_random(data=,vari=,seed=,n=);

%let seed=&seed.;

data temp_random;
set &data.;
keep &vari.;
run;

proc sort data=temp_random nodupkey;
by &vari.;
run;

data temp_random;
set temp_random;
Enumerated=ranuni(&seed.);
run;

proc sort data=temp_random;
by Enumerated;
run;

data temp_random;
set temp_random;
newID_&n.=_N_;
drop Enumerated;
run;

proc sort data=temp_random;
by &vari.;
run;


data lf.lf_&vari._11_02;
set temp_random;
run;

proc sort data=random_ys;
by &vari.;
run;

data random_ys;
merge random_ys (in=a) lf.lf_&vari._11_02 (in=b);
by &vari.;
if a;
run;

data random_ys;
set random_ys;
drop &vari.;
run;


%mend ps_random;

%ps_random(data=random_ys, vari=BENE_ID, seed=05092017, n=1);
%ps_random(data=random_ys, vari=AT_PHYSN_NPI, seed=11132017, n=2);
%ps_random(data=random_ys, vari=AT_PHYSN_UPIN, seed=11142017, n=3);
%ps_random(data=random_ys, vari=OP_PHYSN_NPI, seed=11152017, n=4);
%ps_random(data=random_ys, vari=OP_PHYSN_UPIN, seed=11162017, n=5);
%ps_random(data=random_ys, vari=PRVDR_NUM, seed=11172017, n=6);
%ps_random(data=random_ys, vari=UPIN, seed=11182017, n=7);
%ps_random(data=random_ys, vari=NPI, seed=11192017, n=8);
%ps_random(data=random_ys, vari=ResearchID, seed=11202017, n=9);
%ps_random(data=random_ys, vari=CLM_ID, seed=3272018, n=10);


******************************************************;
**** Split into four transfer different datasets. ****;

data transfer_trad_gensurg transfer_trad_ortho
	transfer_mod_gensurg transfer_mod_ortho;
set random_ys;
if (era_trad=1 and gensurg_adm_trad=1) then output transfer_trad_gensurg;
if (era_trad=1 and ortho_adm_trad=1) then output transfer_trad_ortho;
if (era_mod=1 and gensurg_adm_mod=1) then output transfer_mod_gensurg;
if (era_mod=1 and ortho_adm_mod=1) then output transfer_mod_ortho;
run;

********************************************************************************;
**** Exclude patients from new surgeons who did not have at least 10 cases. ****;

%macro unusable_surg(era=, surg=, id=);

proc sort data=transfer_&era._&surg.;
by &id.;
run;


proc means data=transfer_&era._&surg. noprint;
var count;
by &id.;
output out=temp_surg_sums sum=sum_pats;
run;

data temp_surg_sums (keep=&id.);
set temp_surg_sums;
if sum_pats >= 10;
run;

data transfer_&era._&surg.;
merge transfer_&era._&surg. (in=a) temp_surg_sums (in=b);
by &id.;
if a=1 and b=1;
run;

%mend unusable_surg;

%unusable_surg(era=trad, surg=gensurg, id=newID_7);
%unusable_surg(era=trad, surg=ortho, id=newID_7);
%unusable_surg(era=mod, surg=gensurg, id=newID_8);
%unusable_surg(era=mod, surg=ortho, id=newID_8);


******************************************************************************;
**** Exclude patients from hospitals that did not have at least 1 new and ****;
**** at least 1 exp. surgeon per era and surgery type. ***********************;


%macro unusable_hosp(era=,surg=);

proc sort data=transfer_&era._&surg.;
by newID_6;
run;

proc means data=transfer_&era._&surg. noprint;
var &era._New_Surgeon_flag_numeric &era._Exp_Surgeon_flag_numeric;
by newID_6;
output out=&era._&surg._sums sum=sum_new_surgeons sum_exp_surgeons;
run;



data &era._&surg._sums(keep=newID_6);
set &era._&surg._sums;
multiple = sum_new_surgeons*sum_exp_surgeons;
if multiple > 0;
run;

data transfer_&era._&surg.;
merge transfer_&era._&surg. (in=a) &era._&surg._sums (in=b);
by newID_6;
if a=1 and b=1;
run;

%mend unusable_hosp;

%unusable_hosp(era=trad,surg=gensurg);
%unusable_hosp(era=trad, surg=ortho);
%unusable_hosp(era=mod, surg=gensurg);
%unusable_hosp(era=mod, surg=ortho);


*******************************************;
**** Output the datasets for transfer. ****;

%macro create_transfer_data(era=, surg=);

proc contents data=transfer_&era._&surg.;
run;

option nonotes;
proc export data=transfer_&era._&surg.
	outfile="/Young_Surgeon_data/sas/sharpe/match/transfer_&era._&surg._simul_11_02.csv"
	dbms=CSV replace;
run;
option notes;

title "&era. by Trad_New";
proc freq data=transfer_&era._&surg.;
tables admit_year*Trad_New_Surgeon_flag_numeric;
run;
title "";

title "&era. by Mod New";
proc freq data=transfer_&era._&surg.;
tables admit_year*Mod_New_Surgeon_flag_numeric;
run;
title "";

%mend create_transfer_data;

%create_transfer_data(era=trad, surg=gensurg);
%create_transfer_data(era=trad, surg=ortho);
%create_transfer_data(era=mod, surg=gensurg);
%create_transfer_data(era=mod, surg=ortho);

